package com.z.platform.controller;


import java.io.Serializable;

/**
 * 同意返回对象
 * @author haonan.zhang
 */
public class MyControllerResult<T> implements Serializable {
    private static final long serialVersionUID = 1L;

    private Integer code;

    @Override
    public String toString() {
        return "MyControllerResult{" +
                "code=" + code +
                ", msg='" + msg + '\'' +
                ", data=" + data +
                '}';
    }

    public MyControllerResult(Integer code, String msg, T data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    private String msg;

    private T data;

    public Integer getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    public static <T> MyControllerResult<T> ok() {
        return restResult(null, Constant.SUCCESS, "成功");
    }

    public static <T> MyControllerResult<T> ok(T data) {
        return restResult(data, Constant.SUCCESS, "成功");
    }

    public static <T> MyControllerResult<T> ok(T data, String msg) {
        return restResult(data, Constant.SUCCESS, msg);
    }

    public static <T> MyControllerResult<T> failed() {
        return restResult(null, Constant.FAIL, null);
    }

    /**
     *  默认 code
     * @param msg 自定义msg
     * @param <T>
     * @return
     */
    public static <T> MyControllerResult<T> failed(String msg) {
        return restResult(null, Constant.FAIL, msg);
    }

    /**
     *
     * @param code 自定义 code
     * @param msg 自定义 msg
     * @param <T>
     * @return
     */
    public static <T> MyControllerResult<T> failed(Integer code,String msg) {
        return restResult(null, code, msg);
    }

    public static <T> MyControllerResult<T> failed(T data) {
        return restResult(data, Constant.FAIL, null);
    }

    public static <T> MyControllerResult<T> failed(T data, String msg) {
        return restResult(data, Constant.FAIL, msg);
    }

    public MyControllerResult() {
    }

    public static <T> MyControllerResult<T> restResult(T data, Integer code, String msg) {
        MyControllerResult<T> apiResult = new MyControllerResult<>();
        apiResult.setCode(code);
        apiResult.setData(data);
        apiResult.setMsg(msg);
        return apiResult;
    }

    protected String convert2Json(){
        return "{" +
                "code:" + code +
                ", msg:'" + msg + '\'' +
                ", data:" + data +
                '}';
    }
    static class Constant{
        /**
         * 成功标记
         */
        public static final Integer SUCCESS = 0;
        /**
         * 失败标记
         */
        public static final Integer FAIL = 1;
    }
}
